package com.example.mapper;

import com.example.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Title: spring-boot-druid
 * @Package com.example.mapper
 * @Date 2018/5/7下午1:47
 * @Author PX ye
 * @E-mail yepx2013@gmail.com
 * @Company http://www.yiautos.com
 * @Description: user 的 mapper
 */
@Mapper
public interface UserMapper {

    @Select("SELECT * FROM `user` where id = #{id}")
    User findById(Long id);

    @Select("SELECT * FROM `user` where id = #{id}")
    @Results({
            @Result(property = "address", column = "address_id",
                    one = @One(select = "com.example.mapper.AddressMapper.findAddressById")),
            @Result(property = "cars", column = "id",
                    many = @Many(select = "com.example.mapper.CarMapper.findCarByUserId"))
    })
    User findUserWithAddressAndCar(Long id);

    @Select("select * from user where name = #{name}")
    User findCityByName(@Param("name") String name);

    @Select("select * from user")
    @Results({
            @Result(property = "address", column = "address_id",
                    one = @One(select = "com.example.mapper.AddressMapper.findAddressById")),
            @Result(property = "cars", column = "id",
                    many = @Many(select = "com.example.mapper.CarMapper.findCarByUserId"))
    })
    List<User> findAll();
}
